home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Demos / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Utilities / String Utilities / TranslateChars < prev    next >
Encoding:
Text File  |  1994-02-18  |  1.4 KB  |  37 lines  |  [TEXT/IGR0]

  1. | <TranslateChars>
  2.  
  3. | Returns oldStr after replacing any of oldChars with the corresponding character in newChars.
  4. |
  5. | For example, str=TranslateChars(str,"abc","xyz")    | replace a with x, b with y, and c with z
  6. | If newChars is "", deletes oldChars from str
  7. | Does not check for newChars already in str before translation;
  8. | if you want to delete oldChars, then call TranslateChars(str,oldChars,"") first.
  9. Function/S TranslateChars(oldStr,oldChars,newChars)
  10.     String oldStr,oldChars,newChars
  11.     Variable ndx,whichOldChar=0,whichNewChar=0
  12.     Variable numOldChars=strlen(oldChars),numNewChars=strlen(newChars)
  13.     String oldChar,newChar,newStr=oldStr
  14.     if( (strlen(oldStr)>0) %& (numOldChars>0) )
  15.         do
  16.             ndx= 0    | scan str from start
  17.             oldChar= oldChars[whichOldChar,whichOldChar]    | could be "", then strsearch returns -1
  18.             do
  19.                 ndx= strsearch(oldStr,oldChar,ndx)        | search original string so that swap via Translate(str,"ab","ba") works
  20.                 if( ndx == -1 )
  21.                     break
  22.                 endif
  23.                 newChar= newChars[whichNewChar,whichNewChar]
  24.                 newStr[ndx,ndx]= newChar    | replace in new string, newChar could be ""
  25.                 if( strlen(newChar)==0 )
  26.                     oldStr[ndx,ndx]=""        | keep old and new string character positions synchronized
  27.                 else
  28.                     ndx += 1        
  29.                 endif
  30.             while( 1 )    | exit via break
  31.             whichOldChar += 1
  32.             whichNewChar= mod(whichOldChar,numNewChars)    | when replacement chars are exhausted, start over
  33.         while( whichOldChar < numOldChars )
  34.     endif
  35.     return newStr
  36. End
  37.